<template>
  <el-select v-model="selectedMajorCode" placeholder="请选择所属专业" :clearable="clearable" @change="onSelectChange">
    <el-option
      v-for="major in majorList"
      :key="major.code"
      :label="major.name"
      :value="major.code">
    </el-option>
  </el-select>
</template>

<script>

import {listUserEnabledMajor, listMajor} from "@/api/system/major";

export default {
  name: "MajorSelect",
  props: {
    value: String,

    //只查看用户包含的
    onlyUserContain: {
      type: Boolean,
      default: true
    },

    //只查看启用的
    onlyEnabled: {
      type: Boolean,
      default: true
    },

    //是否允许清除选项
    clearable: {
      type: Boolean,
      default: true
    }
  },

  data() {
    return {
      selectedMajorCode: '',
      //列表
      majorList: []
    };
  },
  created() {
    //设置默认值
    this.selectedMajorCode = this.value;
    //查询列表
    if (this.onlyUserContain) {
      listUserEnabledMajor(!this.onlyEnabled).then(res => {
        this.majorList = res.data;
      })
    } else {
      listMajor(this.onlyEnabled).then(res => {
        this.majorList = res.rows;
      })
    }

  },
  methods: {
    onSelectChange(value) {
      this.$emit("input", value);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
